package cn.exlu.coco.core.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileManager {
    public static final String ENCODE_UTF8="UTF-8";
    public static final String ENCODE_GBK="GBK";

    public static String rootPath(){
        return FileManager.class.getResource("/").getFile().toString();
    }
    
    public static String rootPathSrc(){
        return System.getProperty("user.dir");
    }
    
    public static boolean isUsed(File file){
        if(file.renameTo(file)){
            return false;
        }

        return true;
    }

    public static final boolean writeFile(String filePathName, String content, boolean append){
        return writeFile(filePathName,content,ENCODE_UTF8,append);
    }

    public static final boolean writeFile(String filePathName, String content, String encode, boolean append){
    	File file =new File(filePathName).getParentFile();
    	if(!file.exists()){
    		file.mkdirs();
    	}
    	
    	BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePathName, append), encode));
            if(append){
            	writer.newLine();
            	writer.append(content);
            }else{
            	writer.write(content);
            }

            writer.flush();
        } catch (IOException e) {
            return false;
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    return false;
                }
            }
        }
        
        return true;
    }

    public static final boolean writeFile(String filePathName, String content){
        return writeFile(filePathName, content, ENCODE_UTF8);
    }

    /**
     * 写入文件
     * @param filePathName 文件名全路径
     * @param content 写入的内容
     * @return true:写入成功 false:写入失败
     */
    public static final boolean writeFile(String filePathName, String content, String encode){
    	File file =new File(filePathName).getParentFile();
    	if(!file.exists()){
    		file.mkdirs();
    	}
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePathName), encode));
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    return false;
                }
            }
        }
        
        return true;
    }

    public static final String readFile(String filePathName){
        return readFile(filePathName, ENCODE_UTF8);
    }

    /**
     * JSON文件读取
     * @param path
     * @return
     */
    public static final String readFile(String filePathName, String encode){
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePathName), encode));
            String line = "";
            while(true){
                line = reader.readLine();
                if(line == null){
                    break;
                }
                
                sb.append(line.trim());
            }
            
            
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }finally{
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    return null;
                }
            }
        }
        
        return sb.toString();
    }
    
    /**
     * 获取文件扩展名
     * @param file
     * @return
     */
    public static final String getExtensionName(File file) {
        int index = file.getName().lastIndexOf(".");
        if(index == -1) {
            return "";
        }
        
        return file.getName().substring(index+1);
    }
}
